package com.iceland.server.server.cmd.strategy;

import com.iceland.common.message.CmdMessage;
import com.iceland.common.message.MessageType;
import com.iceland.common.rpc.RpcMessageManager;
import com.iceland.common.strategy.CmdStrategy;
import com.iceland.common.util.AttributeKeyUtils;
import com.iceland.server.util.ServerChannelContext;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MarkerFactory;

/**
 * 命令通道建立
 */
@Slf4j
public class CmdChannelConnectStrategy  implements CmdStrategy {

    @Override
    public void doStrategy(ChannelHandlerContext ctx, CmdMessage m) {
        log.info(MarkerFactory.getMarker(this.getClass().getTypeName()),"命令通道建立,{}连接至{}",ctx.channel().remoteAddress(),ctx.channel().localAddress());

        ServerChannelContext channelContext=(ServerChannelContext)AttributeKeyUtils.getChannelContext(ctx.channel());


        //此处第一次为channel创建ServerChannelContext，RpcMessageManager
        if(channelContext!=null){
            log.warn("channelContext应该为空，项目中可能存在逻辑问题");
        }
        RpcMessageManager rpcMessageManager = new RpcMessageManager(ctx.channel());
        channelContext=new ServerChannelContext(rpcMessageManager,null,null,null);
        AttributeKeyUtils.setChannelContext(ctx.channel(),channelContext);
        log.info(MarkerFactory.getMarker(this.getClass().getTypeName()),"rpc已能正常使用，命令通道与rpc绑定完成,r:{},l:{}",ctx.channel().remoteAddress(),ctx.channel().localAddress());

    }

    @Override
    public int getType() {
        return MessageType.CMD_CHANNEL_CONNECT.getType();
    }
}
